package 剑指offer;

import leetcode.Head;
import leetcode.ListNode;

import java.util.List;

/**
 * {1,2,3,4,5}, 2, 4
 */
public class Offer22_JZ14 {

    public static ListNode FindKthToTail (ListNode pHead, int k) {
        // phead为null
        if(pHead == null || k==0) {
            return null;
        }

        ListNode slow = pHead;
        ListNode fast = pHead;
        while (--k > 0) {
            // k大于链表长度
            if (fast.next == null) {
                return null;
            }
            fast = fast.next;
        }
        while (fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    public static void main(String[] args) {
        ListNode head = Head.genListEven();
        ListNode res = FindKthToTail(head, 2);
        System.out.println(res.val);
    }
}
